package com.cheetah.cloud2020.web;

import com.cheetah.cloud2020.bean.ResponseResult;
import com.cheetah.cloud2020.config.SpringTaskMgr;
import com.cheetah.cloud2020.entity.SystemJob;
import com.cheetah.cloud2020.mapper.SystemJobMapper;
import com.cheetah.cloud2020.service.RibbonJobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/ribbon/job")
public class RibbonJobController {

    @Autowired
    private SystemJobMapper systemJobMapper;

    @Autowired
    private RibbonJobService ribbonJobService;

    @RequestMapping("/addCronTask")
    public ResponseResult addCronTask(@RequestBody SystemJob systemJob) {
        systemJob.setState((short) 1);
        if (systemJob.getId()!=null && systemJob.getId()>0) {
            this.systemJobMapper.updateById(systemJob);
        } else {
            this.systemJobMapper.insert(systemJob);
        }
        return ribbonJobService.addCron(systemJob);
    }

    @RequestMapping("/cancel")
    public ResponseResult cancel(int id) {
        SystemJob job = new SystemJob();
        job.setState((short)0);
        job.setId(id);
        this.systemJobMapper.updateById(job);
        SpringTaskMgr.cancel(id);
        return ResponseResult.ok(null);
    }
}
